package cn.congzhou.crm.web.controller;

import cn.congzhou.crm.common.qo.QueryObject;
import cn.congzhou.crm.common.vo.CommonResultJson;
import cn.congzhou.crm.common.vo.PageTableData;
import cn.congzhou.crm.domain.Department;
import cn.congzhou.crm.service.IDepartmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/department")
public class DepartmentController {

    private Logger log = LoggerFactory.getLogger(DepartmentController.class);

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/toDeptTable")
    public String toDeptTable(){
        return "/department/departmentTable";
    }

    @RequestMapping("/list")
    @ResponseBody
    public CommonResultJson listDepartment(QueryObject qo){
        PageTableData<Department> pageTableData = departmentService.queryDepartment(qo);
        CommonResultJson<PageTableData<Department>> result = CommonResultJson.success(pageTableData);
        log.info("查看部门信息");
        return result;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public CommonResultJson saveOrUpdate(Department department){
        //参数校验
        //...
        departmentService.saveOrUpdate(department);
        return CommonResultJson.success("保存成功");
    }

    @RequestMapping("/delete")
    @ResponseBody
    public CommonResultJson delete(Long id, String deptName){
        departmentService.deleteById(id);
        log.info("删除了" + deptName + "部门");
        return CommonResultJson.success("删除成功");
    }

}
